<?php

defined("_ARKN_EXEC") or exit("no no, mr. superman no here");

require_once ROOT_FOLDER . "/php/configs/database_cfg.php";

class Course {

	private $db;
	private $isDbConnected;
	private $logger;

	public function __construct() {
		$this->logger = Logger::getLogger("Course.php");
		$this->db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
		if($this->db->connect_error)
			throw new Exception("Database connection error");
	}
	
	public function addCourse($departmentid, $coursename, $credits) {
		$this->logger->info("Course.addCourse enter.");
		$success = FALSE;
		$this->logger->info("Validating new course information.");
		if(empty($departmentid) || !is_numeric($departmentid) || empty($coursename) || empty($credits) || !is_numeric($credits))
			$this->logger->info("New course information is not valid.");
		else {
			$this->logger->info("Course information has been validated.");
			$this->logger->info("Preparing statement.");
			if($stmt = $this->db->prepare("insert into courses(departmentid, name, credits)values(?, ?, ?);")) {
				$stmt->bind_param("isi", $departmentid, $coursename, $credits);
				$stmt->execute();
				$stmt->store_result();
				$affectedRows = $stmt->affected_rows;
				$stmt->fetch();
				$stmt->close();
				if($affectedRows == 1) {
					$success = TRUE;
					$this->logger->info("Course" . $coursename . " has been inserted into the database.");
				} else
					$this->logger->error("Error occured while executing the SQL statement.");
			} else
				$this->logger->error("Error occured while preparing the SQL statement.");
		}
		$this->logger->info("Course.addCourse exit.");
		return $success;
	}
	
	public function coursesFromDep($departmentId) {
		$this->logger->info("Course.addCourse enter.");
		$courses = NULL;
		$this->logger->info("Preparing statement.");
		if($stmt = $this->db->prepare("select courseid, name, credits from courses where departmentid = ?;")) {
			$stmt->bind_param("i", $departmentId);
			$stmt->execute();
			$stmt->store_result();
			if(!$stmt->errno) {
				$results = $this->bind_result_array($stmt);
				$courses = array();
				$count = 0;
				while($stmt->fetch())
					$courses[$count++] = $this->getCopy($results);
				$this->logger->info("Read $count courses from the database.");
				$stmt->close();
			} else
				$this->logger->info("Error occured while executing the SQL statement.");
		} else
			$this->logger->error("Error occured while preparing the SQL statement.");
		$this->logger->info("Course.addCourse exit.");
		return $courses;
	}
	
	function bind_result_array($stmt) {
		$meta = $stmt->result_metadata();
		$results = array();
		while ($field = $meta->fetch_field()) {
			$results[$field->name] = NULL;
			$params[] = &$results[$field->name];
		}
		call_user_func_array(array($stmt, "bind_result"), $params);
		return $results;
	}

	function getCopy($row) {
		return array_map(create_function('$a', 'return $a;'), $row);		
	}
	
	public function __destruct() {
		if($this->db != NULL) {
			$this->db->kill($this->db->thread_id);
			$this->db->close();
		}
	}
}

$course = new Course();
?>
